﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Navigation;
using BudgetManager.Domain;
using BudgetManager.UserControls;

namespace BudgetManager
{
    public partial class Income : PhoneApplicationPage
    {
        Category _category;

        private object okB;
        private object editB;
        private object deleteB;
        private object cancelB;
        private bool editBOL;

        public Income()
        {
            editBOL = false;
            InitializeComponent();
            _category = Category.ReadDefaultCategory();
            txtCategory.Text = _category.Name;
            datePicker.Value = DateTime.Now;

            //load data user control
            userControlViewValues.TypeState = UserControlViewValues.State.INCOME;
            userControlViewValues.LoadData();

            okB = ApplicationBar.Buttons[0];
            editB = ApplicationBar.Buttons[1];
            deleteB = ApplicationBar.Buttons[2];
            cancelB = ApplicationBar.Buttons[3];
            ApplicationBar.Buttons.Clear();



            Object category;
            if (PhoneApplicationService.Current.State.TryGetValue("Category", out category) && category != null)
            {
                _category = (Category)category; // save state
                txtCategory.Text = _category.Name; //change text box
                PhoneApplicationService.Current.State["Category"] = null; // Clean memory
            }
        }

        private void onDelete(object sender, EventArgs e)
        {
            if (userControlViewValues.SelectedValue != null)
            {
                AccountManager.SelectedAccount.deleteIncome(userControlViewValues.SelectedValue.Id);
                userControlViewValues.listBoxActivities.ItemsSource = AccountManager.SelectedAccount.GetIncomesOrderByDate();
            }
        }


        private void onOk(object sender, EventArgs e)
        {
            this.Focus();//esconder o teclado
            if (incomesPivot.SelectedIndex == 0)
            {
                BudgetManager.Domain.Income income = new BudgetManager.Domain.Income { Category = _category, Money = Convert.ToDouble(txtMoney.Text), FullDate = datePicker.Value.Value, Description = txtDescription.Text };
                AccountManager.SelectedAccount.addIncome(income);
            }

            NavigationService.Navigate(new Uri(string.Format("/Home.xaml?"), UriKind.Relative));
        }

        private void onCancel(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri(string.Format("/Home.xaml?"), UriKind.Relative));
        }

        private void onTap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            NavigationService.Navigate(new Uri(string.Format("/CategoryIncome.xaml?"), UriKind.Relative));
        }



        private void onTapMoney(object sender, System.Windows.Input.GestureEventArgs e)
        {
            txtMoney.Text = String.Empty;
        }
        private void onchangeonPivot(object sender, PivotItemEventArgs e)
        {
            this.Focus();//esconder o teclado
            if (incomesPivot.SelectedIndex == 1) // view/edit
            {
                ApplicationBar.Buttons.Clear();
                ApplicationBar.Buttons.Add(okB);
                ApplicationBar.Buttons.Add(editB);
                ApplicationBar.Buttons.Add(deleteB);
                ApplicationBar.Buttons.Add(cancelB);
            }
            else    //
            {
                ApplicationBar.Buttons.Clear();
                ApplicationBar.Buttons.Add(okB);
                ApplicationBar.Buttons.Add(cancelB);
            }
        }
        private void onEdit(object sender, EventArgs e)
        {
            if (userControlViewValues.SelectedValue != null)
            {
                PhoneApplicationService.Current.State["abstractvalue"] = userControlViewValues.SelectedValue;
                NavigationService.Navigate(new Uri(string.Format("/EditValue.xaml?"), UriKind.Relative));
                editBOL = true;
            }
        }
        private void lole_GotFocus(object sender, RoutedEventArgs e)
        {
            if (editBOL)
            {

                userControlViewValues.listBoxActivities.ItemsSource = AccountManager.SelectedAccount.GetExpensesOrderByDate();
                editBOL = false;
            }


        }

    }
}
